package day9;

import java.rmi.Remote;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Home extends Thread {
    int id;
    int n=0;
    static int count=0;

    public Home(int id) {
        this.id = id;
    }

    public void run() {
        String name=Thread.currentThread().getName();
            synchronized (Home.class){
                while (n<5){
                if(count%3==id){
                    System.out.println(name);
                    count++;
                    n++;
                    Home.class.notifyAll();
                }else{
                    try {
                        Home.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }
    static StringBuilder result=new StringBuilder();
    public static void main(String[] args) throws InterruptedException {
        //demo();
        //demo1();
        //demo2();
        demo5();
    }
    public static void demo() throws InterruptedException {
        Thread t1=new Thread(()->{
            for(int i=1;i<=100;i++){
                if(i%2==0) {
                    result.append(i+" ");
                }
            }
            System.out.println(Thread.currentThread().getName()+" "+"2的倍数是:"+result);
        },"线程1");
        Thread t2=new Thread(()->{
            for(int i=1;i<=100;i++){
                if(i%3==0) {
                    result.append(i+" ");
                }
            }
            System.out.println(Thread.currentThread().getName()+" "+"3的倍数是:"+result);
        },"线程1");
        Thread t3=new Thread(()->{
            for(int i=1;i<=100;i++){
                if(i%5==0) {
                    result.append(i+" ");
                }
            }
            System.out.println(Thread.currentThread().getName()+" "+"5的倍数是:"+result);
        },"线程1");
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        t3.join();
    }
    public static void demo1(){
        new Thread(()->{
            synchronized (Home.class){
                for(int i=1;i<=52;i++){
                    System.out.print(i);
                    System.out.print(++i);
                    Home.class.notifyAll();
                    try {
                        Home.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(()->{
            synchronized (Home.class){
                for(int i=65;i<=97;i++){
                    System.out.print((char)(i));
                    Home.class.notifyAll();
                    try {
                        Home.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    public static void demo2(){
        new Thread(new Home(0),"A").start();
        new Thread(new Home(1),"B").start();
        new Thread(new Home(2),"C").start();
    }
    public static void demo5(){
        List<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(2);
        list.add(3);
        List<Integer> list1=new ArrayList<>();
        Iterator<Integer> it=list.iterator();
        while (it.hasNext()){
            Integer n=it.next();
            if(list1.contains(n)){
                it.remove();
            }else{
                list1.add(n);
            }
        }
        System.out.println(list);
        System.out.println(list1);
    }

}
